import axios from 'axios';
// import qs from "qs";
// import store from "@/store/index";
// import router from '@/router/index';
// import { dataList } from '@/components/aspin/data';
// import { message } from 'ant-design-vue';
// import { storage } from '../storage/storage';

//数据请求字符
axios.defaults.baseURL = import.meta.env.VITE_PUBLIC_URL,
	// 如果请求话费了超过 `timeout` 的时间，请求将被中断
	axios.defaults.timeout = 5000;
// 表示跨域请求时是否需要使用凭证
axios.defaults.withCredentials = false;
// axios.defaults.headers.common['token'] =  AUTH_TOKEN
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
// 允许跨域
axios.defaults.headers.post["Access-Control-Allow-Origin-Type"] = "*";

// 请求拦截器
axios.interceptors.request.use(function(config) {
	// console.log('axios.interceptors.request', config);
	if (
		config.method === "get" ||
		config.method === "post" ||
		config.method === "put" ||
		config.method === "delete"
	) {
		// qs序列化
		// config.data = JSON.parse(config.data);
	}
	// 若是有做鉴权token , 就给头部带上token
	/*  if (storage.get(store.state.Roles)) {
	    store.state.Roles
	    config.headers.Authorization = storage.get(store.state.Roles);
	  } */
	return config;
}, error => {
	console.error(error.data.error.message);
	return Promise.reject(error.data.error.message);
})

// 响应拦截器
axios.interceptors.response.use(function(config) {
		// dataList.show = true
		if (config.status === 200 || config.status === 204) {
			/* setTimeout(() => {
			  dataList.show = false
			}, 400) */
			return Promise.resolve(config);
		} else {
			return Promise.reject(config);
		}
	},
	function(error) {
		console.log(error);
		if (error.response) {
			return Promise.reject(error.response);
		} else {
			console.error("处理断网的情况==>" + error.response)
		}
	}
)

export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.get(axios.defaults.baseURL+url, {
                params: params
            })
            .then(response => {
                resolve(response.data);
            })
            .catch(err => {
                reject(err)
            })
    })
}

export function post(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.post(axios.defaults.baseURL+url, data)
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err)
            })
    })
}

// export default axios